<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class GenerateModel extends Command
{
    /**
     * 命令
     * @var string
     */
    protected $signature = 'generate:model {model=_?!} {connection=connection_name} {table=table_name}';


    /**
     * 命令描述
     * @var string
     */
    protected $description = '生成基于BaseModel的模型类';


    /**
     * 依赖注入文件系统
     * @var Filesystem
     */
    protected $file;


    /**
     * 模型名称
     * @var string
     */
    protected $model;


    /**
     * 数据库连接
     * @var string
     */
    protected $connection;


    /**
     * 模型对于数据表名
     * @var string
     */
    protected $table;


    /**
     * 模型的模板文件
     * @var string
     */
    protected $template_path = "/stubs/base_model.stub";

    /**
     * 生成文件扩展名
     * @var string
     */
    protected $ext = ".php";


    /**
     * GenerateModel constructor.
     * @param Filesystem $file
     */
    public function __construct(Filesystem $file)
    {
        $this->file = $file;

        $this->template_path = base_path('resources') . $this->template_path;

        parent::__construct();


    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->model = ucfirst($this->argument('model'));
        $this->connection = $this->argument('connection');
        $this->table = $this->argument('table');

        // 判断参数是否为空
        if ($this->model === '_?!') {
            $this->emptyModelError();
        }


        $content = $this->replaceClassName([
            '$model_name' => $this->model,
            '$connection_name' => $this->connection,
            '$table_name' => $this->table,
        ], $this->getStub());

        // 判断是否生成成功
        if ($this->createClass($this->model, $content)) {
            $this->generateSuccess();
        }

    }

    /**
     * 获取要生成的类文件的模板
     * @return bool|string
     */
    protected function getStub()
    {

        return file_get_contents($this->template_path);
    }

    /**
     * 替换模板中的类名称，表名等
     * @param array $params
     * @param string $template
     * @return mixed|string
     */
    protected function replaceClassName(array $params, string $template)
    {
        foreach ($params as $key => $param) {
            $template = str_replace($key, $param, $template);
        }

        return $template;
    }

    /**
     * 生成文件
     * @param $class_name
     * @param $content
     * @return int
     */
    protected function createClass($class_name, $content)
    {
        return $this->file->put(app_path('Models' . "/" . $class_name . '.php'), $content);
    }

    /**
     * model参数为空的提示
     */
    protected function emptyModelError()
    {
        $this->error('Error 模型名称参数不能为空!');
        $this->comment('----------命令----------');
        $this->comment('php artisan generate:model model_name [connection_name] [table_name]');
        exit();
    }

    /**
     * model生成成功的提示
     */
    protected function generateSuccess()
    {
        $this->info('Success!');
        $this->comment('--------------------');
        $this->comment("app\\Models\\" . $this->model . $this->ext);
    }
}
